package org.way.base.util;

import cn.hutool.core.util.IdUtil;

import java.time.DayOfWeek;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class DateUtil {

    /**
     * 获取本月第一天
     */
    public static String getFirstDayOfMonth() {
        LocalDate firstDayOfMonth = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
        return toStr(firstDayOfMonth);
    }

    /**
     * 获取本月最后一天
     */
    public static String getLastDayOfMonth() {
        LocalDate lastDayOfMonth = LocalDate.now().with(TemporalAdjusters.lastDayOfMonth());
        return toStr(lastDayOfMonth);
    }

    /**
     * 获取本周一的日期
     */
    public static String getMondayOfWeek() {
        LocalDate now = LocalDate.now();
        LocalDate monday = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
        return toStr(monday);
    }

    /**
     * 获取本周日的日期
     */
    public static String getSundayOfWeek() {
        LocalDate now = LocalDate.now();
        LocalDate monday = now.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
        return toStr(monday);
    }

    /**
     * 获取上周一和周日的日期
     */
    public static Map<String,String> getLastWeek() {
        LocalDate now = LocalDate.now();
        LocalDate weekMonday = now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
        LocalDate lastWeekMonday = weekMonday.minusWeeks(1);
        LocalDate lastWeekSunday = lastWeekMonday.plusDays(6);

        Map<String, String> map = new HashMap<>();
        map.put("lastWeekMonday",toStr(lastWeekMonday));
        map.put("lastWeekSunday",toStr(lastWeekSunday));
        return map;
    }

    public static String toStr(LocalDate localDate) {
        return localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }

    public static String getToday() {
        return toStr(LocalDate.now());
    }

    public static LocalDate toDate(String strDate) {
        return LocalDate.parse(strDate,DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }

    /**
     * 获取本月每周日的日期
     */
    public static List<LocalDate> getSundayOfMonth() {
        LocalDate today = LocalDate.now();
        LocalDate firstDayOfMonth = today.with(TemporalAdjusters.firstDayOfMonth());
        List<LocalDate> sundays = new ArrayList<>();
        LocalDate currentDate = firstDayOfMonth;
        while (currentDate.getMonth() == today.getMonth()) {
            if (currentDate.getDayOfWeek() == DayOfWeek.SUNDAY) {
                sundays.add(currentDate);
            }
            currentDate = currentDate.plusDays(1);
        }
        return sundays;
    }

    /**
     * 获取本月不是完整周的日期
     */
    public static List<String> getDaysOfWeek() {
        List<String> dateStrList = new ArrayList<>();

        LocalDate firstDayOfMonth = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
        if(firstDayOfMonth.getDayOfWeek() != DayOfWeek.MONDAY) {
            int count = DayOfWeek.SUNDAY.getValue() - firstDayOfMonth.getDayOfWeek().getValue();
            for(int i=0;i<=count;i++) {
                String dateStr = DateUtil.toStr(firstDayOfMonth.plusDays(i));
                dateStrList.add(dateStr);
            }
        }

        LocalDate lastDayOfMonth = LocalDate.now().with(TemporalAdjusters.lastDayOfMonth());
        if(lastDayOfMonth.getDayOfWeek() != DayOfWeek.SUNDAY) {
            for(int i=0;i<7;i++) {
                LocalDate localDate = lastDayOfMonth.minusDays(i);
                dateStrList.add(DateUtil.toStr(localDate));
                if(localDate.getDayOfWeek().equals(DayOfWeek.MONDAY)) {
                    break;
                }
            }
        }

        return dateStrList;
    }

    public static LocalDate toLocalDate(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    }
    public static void main(String[] args) {
        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
        System.out.println(IdUtil.simpleUUID());
        System.out.println(TimeUnit.SECONDS);

        AtomicInteger atomicInt = new AtomicInteger(0);

        // 使用lambda表达式定义要执行的任务
        Runnable task = () -> {
            if (atomicInt.get() >= 3) {
                System.out.println("ready quit");
                scheduler.shutdown(); // 关闭调度器
                return; // 退出任务执行
            }

            System.out.println("hello world");
            atomicInt.set(atomicInt.get()+1); // 增加打印次数

            scheduler.schedule(() -> {
            }, 5, TimeUnit.SECONDS); // 改为2秒以模拟
        };
        scheduler.scheduleAtFixedRate(task, 0, 5, TimeUnit.SECONDS);
    }
}
